import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:math';

class DotsIndicator extends AnimatedWidget {
  DotsIndicator(
      {this.controller,
      this.color,
      this.unSelectColor,
      this.itemCount,
      this.onPageSelected})
      : super(listenable: controller);
  final PageController controller;
  final int itemCount;
  final ValueChanged<int> onPageSelected;
  final Color color;
  final Color unSelectColor;
  static const double _kDotSize = 7.0;
  static const double _kMaxZoom = 1.5;
  static const double _kDotSapce = 20.0;

  Widget _buildDot(int index) {
    double selectedness = Curves.easeOut.transform(
      max(
        0.0,
        1.0 - ((controller.page ?? controller.initialPage) - index).abs(),
      ),
    );
    double zoom = 1.0 + (_kMaxZoom - 1.0) * selectedness;
    return new Container(
      width: _kDotSapce,
      child: Center(
        child: new Material(
          color: selectedness == 0 ? unSelectColor : color ?? Colors.white,
          type: MaterialType.circle,
          child: Container(
            width: _kDotSize * zoom,
            height: _kDotSize * zoom,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: new List<Widget>.generate(itemCount, _buildDot),
    );
  }
}
